package com.wuzihao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuzihao.reggie.common.R;
import com.wuzihao.reggie.dto.DishDto;
import com.wuzihao.reggie.dto.SetmealDto;
import com.wuzihao.reggie.entity.Dish;
import com.wuzihao.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    DishService dishService;

    /**&
     * 新增菜品
     * 前端传输过来的json数据，所以用requestBody
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);

        return R.success("菜品新增成功");
    }

    //ajax请求发送过来携带参数
    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        Page<DishDto> pageInfo = dishService.pageInfo(page,pageSize,name);

        return R.success(pageInfo);
    }

    /**&
     * 修改菜品时根据id查询菜品信息，使得修改时页面可以直接回显当前菜品的数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);

        return R.success(dishDto);
    }

    //TODO 如何区分前端发送的数据格式是否是json
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);

        return R.success("菜品修改成功");
    }

    @GetMapping("/list")
    //http://localhost:8080/dish/list?categoryId=1397844391040167938
    public R list(Dish dish){
        List<DishDto> list = dishService.listDish(dish);

        return R.success(list);
    }
}
